// pages/staff/stupeople/writecall.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require("../../../utils/share.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        follow_index: 0,
        followlist: [{
                type: 0,
                name: '请选择跟进方式'
            },
            {
                type: 7,
                name: '打电话'
            },
            {
                type: 8,
                name: '微信'
            },
            {
                type: 9,
                name: '短信'
            },
            {
                type: 10,
                name: '其他'
            }
        ],
        datetime: '2019-01-01 13:37',
        startDate: '',
        endDate: '2099-12-31 23:59',
        placeholder: '请选择时间',
        imgpic: [],
        imgUrl: CONFIG.API_PIC,
        crmid: '',
        consult_id: '',
        id: '',
        textreload: '',
        butfalse:false,
    },
    /**
     * 日历控件绑定函数 
     * 点击日期返回
     */
    onPickerChange: function(e) {
        // console.log(e.detail);
        this.setData({
            datetime: e.detail.dateString,
            placeholder: e.detail.dateString
        })
        // console.log(this.data.datetime)
    },


    // 选择跟进方式
    followchange(e) {
        // console.log(e);
        var that = this;
        that.setData({
            follow_index: e.detail.value
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var id = options.id;
        share.newagainnow1(function(res) {
            that.setData({
                datetime: res,
                crmid: options.crmid,
                consult_id: options.consult_id,
                id: options.id
            })
        })
        share.newagainnow(function(res) {
            // var strst = res;
        }, function(res1) {
            // var strst1 = res1;
        }, function(resobj) {
            // console.log(resobj)
            that.setData({
                startDate: resobj
            })

        })
        if (Number(id) >= 0) {
            wx.showLoading({
                title: '加载中~',
            })
            share.wxRequest('crmapi/user/recordInfo', {
                id: id
            }, function(res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    // console.log(data);

                    var followlist = that.data.followlist;
                    var type = data.type;
                    for (var i = 0; i < followlist.length; i++) {
                        if (Number(type) == followlist[i].type) {
                            var follow_index = i
                        }
                    }
                    if (data.next_time != '') {
                        var next_time = data.next_time;
                    } else {
                        var next_time = '请选择时间';
                    }
                    if (data.imgs != '') {
                        var imgs = data.imgs
                    } else {
                        var imgs = [];
                    }
                    that.setData({
                        imgpic: imgs,
                        placeholder: next_time,
                        follow_index: follow_index,
                        textreload: data.remarks
                    })
                }
                wx.hideLoading();
            })
        }

    },

    // 上传图片
    uploadImage() {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function(res) {
                // console.log(res)
                var tempFilePaths = res.tempFilePaths;
                var photoSrc = res.tempFilePaths[0];
                var filePath = photoSrc;
                share.uploadpic(filePath, function(res) {
                    wx.showToast({
                        title: '成功',
                        icon: 'none'
                    })
                    var imgarr = that.data.imgpic;
                    imgarr.push(res.data.path);
                    // console.log(imgarr);
                    that.setData({
                        imgpic: imgarr
                    })
                    // console.log(res)
                }, function(res) {
                    wx.showToast({
                        title: '上传失败，请重新上传',
                        icon: 'none'
                    })
                })

            }
        })
    },

    // 点击提交
    bindFormSubmit(e) {
        var that = this;
        // console.log(e);
        var textarea1 = e.detail.value.textarea;
        if (Number(that.data.follow_index) == 0) {
            wx.showToast({
                title: '请选择跟进方式',
                icon: 'none'
            })
        } else if (textarea1 == '') {
            wx.showToast({
                title: '请填写跟进内容',
                icon: 'none'
            })
        } else {
            if (!that.data.butfalse){
                that.setData({
                    butfalse:true,
                })
                var imgpic = that.data.imgpic;
                if (Number(imgpic.length) > 0) {
                    var str = imgpic.join(',');
                } else {
                    var str = '';
                }
                // datetime
                if (that.data.placeholder == '请选择时间') {
                    var datetime = ''
                } else {
                    var datetime = that.data.datetime;
                }
                // console.log(datetime);
                // console.log(that.data.placeholder);
                if (Number(that.data.id) >= 0) {
                    var obj = {
                        id: that.data.id, //	是	int	crm表ID
                        remarks: textarea1, //	是	string	跟进内容
                        next_time: datetime, //	是	int	下次跟进时间
                        type: that.data.followlist[that.data.follow_index].type, //	是	int	1:打电话 2:微信 3:短信 4:其他
                        imgs: str, //	否	int	多张图片用英文逗号分开
                    }
                    var url = 'crmapi/user/editStuRecord'
                } else {
                    var obj = {
                        consultant_id: that.data.consult_id, //	是	int	课程顾问ID
                        crm_id: that.data.crmid, //	是	int	crm表ID
                        remarks: textarea1, //	是	string	跟进内容
                        next_time: datetime, //	是	int	下次跟进时间
                        type: that.data.followlist[that.data.follow_index].type, //	是	int	1:打电话 2:微信 3:短信 4:其他
                        imgs: str, //	否	int	多张图片用英文逗号分开
                    }
                    var url = 'crmapi/user/addStuRecord'
                }
                share.wxRequest(url, obj, function(res) {
                    if (Number(res.data.errcode) == 0) {
                        wx.showToast({
                            title: '成功',
                            icon: 'none'
                        })
                        // that.setData({
                        //     butfalse: false,
                        // })
                        wx.setStorageSync('followtype', 2);
                        setTimeout(() => {
                            wx.navigateBack();
                        }, 500);
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        })
                        that.setData({
                            butfalse: false,
                        })
                    }
                })
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})